﻿using System.Collections.Generic;
using System.Linq;

namespace NJabber.Stream.Elements
{
    public static class IqStanzaExtensions
    {
        public static IqQueryElement GetQueryElement(this IqStanza stanza)
        {
            return stanza.FindElement<IqQueryElement>("query");
        }

        public static bool HasQuery(this IqStanza stanza, string queryXmlns = "")
        {
            IqQueryElement query = stanza.FindElement<IqQueryElement>("query");
            if (query == null) return false;
            return query.ContentNamespace.Uri == queryXmlns;
        }

        public static bool HasRosterQuery(this IqStanza stanza)
        {
            return stanza.HasQuery(XmppNamespaces.Roster);
        }

        public static bool IsRosterResult(this IqStanza stanza)
        {
            return stanza.HasQuery(XmppNamespaces.Roster) && stanza.Type == IqStanzaTypes.Result;
        }

        public static bool IsRosterPush(this IqStanza stanza)
        {
            return stanza.HasRosterQuery() && stanza.Type == IqStanzaTypes.Set;
        }

        public static ICollection<RosterItemElement> GetRosterItemsElements(this IqStanza stanza)
        {
            List<RosterItemElement> items = new List<RosterItemElement>();
            if (stanza.HasRosterQuery())
            {
                IqQueryElement query = stanza.GetQueryElement();
                items.AddRange(query.Content.OfType<RosterItemElement>());
            }
            return items;
        }
    }
}
